*****************************************************************
*																*
*				Title: 2_regressions_cotton		 	 	   	*		
*															    *
*****************************************************************

/*  ================================  NOTES  ================================

+ PURPSOSE:
	
	In this do-file we run regressions for the cotton sector and output the regressions' results
	

+ Inputs:

		* 1. panel_cotton.dta
			

==============================  TOP MATTER ==============================*/



************** END HEADER **************


	*DATA
	global proc_data "`pdir'/data"
	global output "`pdir'/outputs/main_paper"

********************************** END HEADER ********************************

use "$proc_data/panel_cotton.dta", clear

********************************************************************************
******************************       FIGURES	 	****************************
********************************************************************************


************	Figure 1 (Panel A). ********************************************
*** Changes in the Productivity Distributions in Cotton


twoway (kdensity logYoverL_real if year == 1806, bwidth(0.2) lcolor(gs6) lpattern(dash) lwidth(thick)) ///
 (kdensity logYoverL_real if year == 1840, bwidth(0.2) lcolor(navy) lpattern(solid) lwidth(thick)), ///
 ytitle(Kernel density, size(large)) xtitle(ln(Output per worker), size(large)) xscale(range(2 10)) xlabel(2(2)10, labsize(medlarge)) ylabel(, labsize(medlarge)) ///
 legend(order(1 "1806" 2 "1840") position(9) ring(0) cols(1) size(large))  scheme(s1color)
	graph export "$output/Figures/Figure_1A.eps", replace
	graph export "$output/Figures/Figure_1A.pdf", replace 



************	Figure 2 (Panel A). ********************************************
*** Productivity Growth at Different Quantiles of the Distribution 	


forv i=10(10)90{
	preserve	
		xi:qreg logYoverL_real_pa i.year, quantile(`i') vce(robust)
		est store est`i'
	restore
}


coefplot est10 || est20 || est30 || est40 || est50 || est60 || est70 || est80 || est90, keep(_I*) vertical pstyle(p7) color(black) ciopts(color(black) recast(rcap)) citop legend(off) xtitle("Decile", size(large)) ytitle(Avg annual growth rate (%), size(large))  ylabel(0 (1) 4, labsize(medlarge))  scheme(s1color) bycoefs bylabels("0.1" "0.2" "0.3" "0.4" "0.5" "0.6" "0.7" "0.8" "0.9")


graph export "$output/Figures/Figure_2A.eps", replace
graph export "$output/Figures/Figure_2A.pdf", replace


***************************************************************************
*********************************		TABLES	 	***********************
***************************************************************************

	
************	Table 1 *************************************
*** (Panel A). Annual Productivity Growth (in %) at Different Quantiles of the Distribution	 	***********

estimates clear
quietly{
	eststo: xi:reg logYoverL_real_pa i.year,  vce(robust) 
local N_obs = e(N)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_1A.tex", replace label ///
	drop(_cons) order($show ) rename (_Iyear_1840 "Spinning \ (1806-1840)") ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") ///
	end("& `N_obs'" "\\[-0.15cm]")  varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) ///
	starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) ///
	collabels(none) 		

*Redundancy in reported number of observations in the last column is fixed by hand after generating the table		


	
************	Table 3.  *************************************
*** Productivity and Plants' Age Profile	in 1806 	***********

estimates clear
quietly{
	eststo: reg logYoverL_real firm_young  if year == 1806, r
	eststo: reg logYoverL_real firm_young logK_over_L  if year == 1806, r
	eststo: reg logYoverL_real firm_young logL_total  if year == 1806, r	
	eststo: reg logYoverL_real firm_young spindle_lowtech  if year == 1806, r
	eststo: reg logYoverL_real firm_young spindle_medtech  if year == 1806, r	
	eststo: reg logYoverL_real firm_young spindle_hightech  if year == 1806, r
}
estout using "$output/Tables/Table_3.tex", replace label drop (_cons) order($show ) ///
prefoot("\hline") stats(r2 N, layout(@ "\multicolumn{1}{c}{@}") fmt(2 %9.0g) labels("R$^2$" "N")) ///
mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.1cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.2cm] ")) ///
cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 

	

